package ${tempValues.package_this_file};

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;
import org.springframework.kafka.listener.ContainerProperties;
import org.springframework.kafka.listener.MessageListener;

import java.util.HashMap;
import java.util.Map;


@Configuration
public class KafkaConsumerConfig {

    // common
<#if kafka?? && kafka.bootstrapServers??>
    @Value("${kafka.bootstrapServers}")
    private String bootstrapServers;
<#else >
    @Value("${r'${bootstrap.servers}'}")
    private String bootstrapServers;
</#if>
<#if kafka?? && kafka.topics??>
    @Value("${kafka.topics}")
    private String topics;
<#else >
    @Value("${r'${topics}'}")
    private String topics;
</#if>

    // consumer
<#if kafka?? && kafka.groupId??>
    @Value("${kafka.groupId}")
    private String groupId;
<#else >
    @Value("${r'${group.id}'}")
    private String groupId;
</#if>
    @Value("${r'${enable.auto.commit:false}'}")
    private String enableAutoCommit;
    @Value("${r'${auto.commit.interval.ms:3000}'}")
    private String autoCommitIntervalMs;
    @Value("${r'${session.timeout.ms:60000}'}")
    private String sessionTimeoutMs;
    @Value("${r'${request.timeout.ms:61000}'}")
    private String requestTimeoutMs;
    @Value("${r'${consume.concurrency:1}'}")
    private Integer consumeConcurrency;
    @Value("${r'${key.deserializer:org.apache.kafka.common.serialization.StringDeserializer}'}")
    private String keyDeserializer;
    @Value("${r'${value.deserializer:org.apache.kafka.common.serialization.StringDeserializer}'}")
    private String valueDeserializer;

    @Bean
    public DefaultKafkaConsumerFactory kafkaConsumerFactory() {
        Map<String, Object> configs = new HashMap<>();
        configs.put("bootstrap.servers", bootstrapServers);
        configs.put("group.id", groupId);
        configs.put("enable.auto.commit", enableAutoCommit);
        configs.put("auto.commit.interval.ms", autoCommitIntervalMs);
        configs.put("session.timeout.ms", sessionTimeoutMs);
        configs.put("request.timeout.ms", requestTimeoutMs);
        configs.put("key.deserializer", keyDeserializer);
        configs.put("value.deserializer", valueDeserializer);
        DefaultKafkaConsumerFactory defaultKafkaConsumerFactory = new DefaultKafkaConsumerFactory<>(configs);
        return defaultKafkaConsumerFactory;
    }

    @Bean
    @ConditionalOnBean(MessageListener.class)
    public ContainerProperties containerProperties(MessageListener messageListener) {
        ContainerProperties containerProperties = new ContainerProperties(topics.split(","));
        containerProperties.setMessageListener(messageListener);
        return containerProperties;
    }

    @Bean
    @ConditionalOnBean(ContainerProperties.class)
    public ConcurrentMessageListenerContainer concurrentMessageListenerContainer(DefaultKafkaConsumerFactory kafkaConsumerFactory, ContainerProperties containerProperties) {
        ConcurrentMessageListenerContainer concurrentMessageListenerContainer = new ConcurrentMessageListenerContainer<>(kafkaConsumerFactory, containerProperties);
        concurrentMessageListenerContainer.setConcurrency(consumeConcurrency);
        return concurrentMessageListenerContainer;
    }

}
